package net.huashitong.supervision.task.service;

import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.*;
import net.huashitong.supervision.task.filter.SpecialTaskFilter;
import net.huashitong.supervision.task.util.WeekDayUtil;
import net.huashitong.synthetical.newSupervision.util.HolidayUtils;
import net.huashitong.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/22
 * Time: 20:14
 * To change this template use File | Settings | File Templates.
 * Description:定时调度任务,自动生成多时限的单位任务
 */

@Service
public class TaskUnitScheduleService {
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private TimeLimitService timeLimitService;
    @Autowired
    private TaskSubService taskSubService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private TaskUserService taskUserService;
    @Autowired
    private SMSUtilService smsUtilService;
    @Autowired
    private SpecialTaskService specialTaskService;

    //定时查询是否有多时限的单位任务需要生成
    public void taskUnitTimes() throws ParseException {
        //查询任务时限表所有未结束的数据
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String timeNow = dateFormat.format(Calendar.getInstance().getTime());
        System.out.println("当前的日期： " + timeNow);
        //取出当前天是几号
        //当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        //当前日
        int nowYear = calendar.get(Calendar.YEAR);//年
        int nowMonth = calendar.get(Calendar.MONTH) + 1;//月
        int day = calendar.get(Calendar.DAY_OF_MONTH);//日
        String yearStr = "";
        String monthStr = nowMonth + "";
        String dayStr = day + "";
        if (nowMonth < 10) {
            monthStr = 0 + monthStr;
        }
        if (day < 10) {
            dayStr = 0 + dayStr;
        }
        //未结束的项目
        List<TimeLimit> timeLimits = timeLimitService.queryState("1");
        if (ObjectUtils.isNotBlank(timeLimits)) {
            for (TimeLimit timeLimit : timeLimits) {
                String timeType = timeLimit.getTimeType();
                /*
                 * 特殊任务多时限生成;
                 * */

                //判断是否有特殊任务时限
                TaskSub taskSub = null;
                SpecialTask specialTask = null;
                if (StringUtils.isNotBlank(timeLimit.getSpecialTaskId())) {
                    //查询最新特殊任务

                    specialTask = specialTaskService.getById(timeLimit.getSpecialTaskId());
                    SpecialTask task = new SpecialTask();
                    if (ObjectUtils.isNotBlank(specialTask)) {
//                        TaskMain taskMain = taskMainService.getById(specialTask.getTaskMainId());
                        SpecialTaskFilter specialTaskFilter = new SpecialTaskFilter();
                        specialTaskFilter.setTaskMainId(specialTask.getTaskMainId());
                        //最新轮次
                        List<SpecialTask> list = specialTaskService.queryFilter(specialTaskFilter);
                        Collections.sort(list, new Comparator<SpecialTask>() {
                            @Override
                            public int compare(SpecialTask o1, SpecialTask o2) {
                                if (o2.getRoundLimit().compareTo(o1.getRoundLimit()) > 0) {
                                    return 1;
                                } else {
                                    return -1;
                                }
                            }
                        });
                        //最新轮次任务
                        specialTask = list.get(0);
                        int size = list.size();
                        String roundLimit = specialTask.getRoundLimit();//任务时限
                       // timeType;  //时限类型 单时限1； 周期2 ； 多时限
                       // period;    //周期：周1月2季度3
                        if (timeNow.equals(roundLimit)) {
                            if ("2".equals(timeType)) {
                                if ("0".equals(timeLimit.getPeriod())) {
                                    //上一轮督查任务的时限
                                    int i = Integer.parseInt(timeLimit.getDay());
                                    //设置任务时限 将天数转换成日期
                                    List<String> holidays = new ArrayList<>();
                                    Date date = new Date();
                                    Date datetime = HolidayUtils.getScheduleActiveDate(date, holidays, i);
                                    //下个任务的时限
                                    String format = dateFormat.format(datetime);
                                    BeanUtils.copyProperties(specialTask, task);
                                    task.setRoundLimit(format);
                                    task.setTimeRound(size + 1 + "");

                                    task.setSysId(null);
                                    task.setOverdueDay(null);
                                    task.setType("1");
                                    task.setStatus("1");
                                    specialTaskService.save(task);

                                } else if ("1".equals(timeLimit.getPeriod())) {
                                    int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                                    if (week < 0)
                                        week = 7;
                                    if (Integer.toString(week).equals(timeLimit.getWeek())) {
                                        //如果当天是周期天。那就生成一个同样的单位任务
                                        //查询当前第一轮 的子任务；
                                        //下周的周一周日
                                        Map<String, String> nextWeek = WeekDayUtil.getNextWeek(dateFormat.format(new Date()));
                                        //下周周期天的日期
                                        String[] dates = WeekDayUtil.getDates(nextWeek.get("beginDateNext"), nextWeek.get("endDateNext"), taskSub.getWeek());
                                        BeanUtils.copyProperties(specialTask, task);
                                        task.setRoundLimit(dates[0]);
                                        task.setTimeRound(size + 1 + "");

                                        task.setSysId(null);
                                        task.setOverdueDay(null);
                                        task.setType("1");
                                        task.setStatus("1");
                                        specialTaskService.save(task);
                                    }
                                } else if ("2".equals(timeLimit.getPeriod())) {
                                    {
                                        //如果是月周期，判断当前天数。取出当前周期月的日
                                        String month = timeLimit.getMonth();
                                        int monthInt = Integer.parseInt(month);
                                        //获取当前月的天数（本月最后一天日期）
                                        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                                        //判断当周期是在本月是否有该日
                                        if (monthInt > actualMaximum) {
                                            //本月没有周期中的日期,如果当天是本月最后一天
                                            if (day == actualMaximum) {
                                                //设置本次任务 的下月时限
                                                nowMonth = nowMonth + 1;
                                                if (nowMonth < 10) {
                                                    monthStr = "0" + nowMonth;
                                                }
                                               // String time = nowYear + "-" + monthStr + "-" + day;
                                                String time=DateUtil.getCurrentDate();
                                                 BeanUtils.copyProperties(specialTask, task);
                                                task.setRoundLimit(time);
                                                task.setTimeRound(size + 1 + "");
                                                task.setSysId(null);
                                                task.setOverdueDay(null);
                                                task.setType("1");
                                                task.setStatus("1");
                                                specialTaskService.save(task);
                                            }
                                        } else {
                                            //本月有这天,当今天是周期日的时候生成下月任务
                                            if (day == monthInt) {
                                                String time = "";
                                                //如果是12月
                                                if (nowMonth == 12) {
                                                    time = (nowYear + 1) + "-01-" + dayStr;
                                                } else if (nowMonth == 1 && monthInt >= 29) {
                                                    //如果当前月是一月并且周期的日时间大于29，那么将下月的时限定为二月28
                                                    time = nowYear + "-02-28";
                                                } else if (actualMaximum % 2 == 1 && monthInt > 30 && nowMonth != 1 && nowMonth != 12) {
                                                    //如果本月是大月，并且周期的日为31号。下月的反馈时限定为30号
                                                    time = nowYear + "-" + (nowMonth + 1) + "-30";
                                                } else {
                                                    // .月时限
                                                    time = nowYear + "-" + (nowMonth + 1) + "-" + dayStr;
                                                }
                                                //设置本次任务 的下

                                                BeanUtils.copyProperties(specialTask, task);
                                                task.setRoundLimit(time);
                                                task.setTimeRound(size + 1 + "");
                                                task.setSysId(null);
                                                task.setOverdueDay(null);
                                                task.setType("1");
                                                task.setStatus("1");
                                                specialTaskService.save(task);
                                            }
                                        }
                                    }
                                } else {
                                   /* long nowHm = dateFormat.parse(timeNow).getTime();//当前日期的毫秒值
                                    long result = nowHm + (24 * 60 * 60 * 1000 * 91); //一个季度后的毫秒值
                                    Date date = new Date(result);
                                    String format = dateFormat.format(date);*/
                                    String nextQuarterDay = DateUtil.nextQuarterDay(timeNow);
                                    BeanUtils.copyProperties(specialTask, task);
                                    task.setRoundLimit(nextQuarterDay);
                                    task.setTimeRound(size + 1 + "");
                                    task.setSysId(null);
                                    task.setOverdueDay(null);
                                    task.setType("1");
                                    task.setStatus("1");
                                    specialTaskService.save(task);
                                }
                            } else if ("3".equals(timeType)) {
                                {
                                    //判断是否有多时限
                                    String limits = timeLimit.getTimeLimits();
                                    if (limits.contains(",")) {
                                        //如果是多时限，将时限转化为数组
                                        String[] split = limits.split(",");
                                        //循环数组
                                        for (int i = 0; i < split.length; i++) {
                                            //如果当前时间与时相同
                                            if (split[i].equals(timeNow)) {
                                                //创建下个时限的任务
                                                if (i < split.length - 1) {
                                                    //取当前时间的下一个时间作为新任务的时限
                                                    String time = split[i + 1];
                                                    BeanUtils.copyProperties(specialTask, task);
                                                    task.setRoundLimit(time);
                                                    task.setTimeRound(size + 1 + "");
                                                    task.setSysId(null);
                                                    task.setOverdueDay(null);
                                                    task.setType("1");
                                                    task.setStatus("1");
                                                    specialTaskService.save(task);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                } else if (StringUtils.isNotBlank(timeLimit.getTaskSubId())) {
                    taskSub = taskSubService.getById(timeLimit.getTaskSubId());
                }
                if (ObjectUtils.isNotBlank(taskSub)) {
                    //查询当前任务
                    TaskMain taskMain = taskMainService.getById(taskSub.getTaskMainId());
                    //查询当前的第一轮单位任务
                    List<TaskUnit> taskUnits = taskUnitService.getBySubId(timeLimit.getTaskSubId());
                    if (ObjectUtils.isBlank(taskUnits)) {
                        continue;
                    }
                    //判断任务时限类
                    if ("2".equals(timeType)) {
                        //如果是周期性的任务，判断是什么周期
                        if ("0".equals(timeLimit.getPeriod())) {
                            for (TaskUnit taskUnit : taskUnits) {
                                //查询当前单位任务最新一轮的时限
                                int maxCount = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());
                                //创建一个内容相同的单位任务
                                //取出上一轮督查任务
                                List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), maxCount);
                                //上一轮督查任务的时限
                                if(ObjectUtils.isBlank(taskUnitList)){
                                    continue;
                                }
                                String limit = taskUnitList.get(0).getTimeLimit();
                                //查询上一轮单位任务批转的用户
                                String taskUnitId = taskUnitList.get(0).getSysId();
                                List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);


                                if (timeNow.equals(limit)) {
                                    int i = Integer.parseInt(timeLimit.getDay());
                                    //设置任务时限 将天数转换成日期
                                    List<String> holidays = new ArrayList<>();
                                    Date date = new Date();
                                    Date datetime = HolidayUtils.getScheduleActiveDate(date, holidays, i);
                                    //下个任务的时限
                                    String format = dateFormat.format(datetime);
                                    //创建一个内容相同的单位任务
                                    TaskUnit newTaskUnit = new TaskUnit();
                                    BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                    newTaskUnit.setSysId(null);
                                    newTaskUnit.setOverdueDay(null);
                                    newTaskUnit.setTimeLimit(format);
                                    newTaskUnit.setScore(null);
                                    newTaskUnit.setStatus("2");

                                    if (ObjectUtils.isNotBlank(taskUsers)) {
                                        newTaskUnit.setType("1");//已批转
                                        newTaskUnit.setState("1");//已办
                                    } else {
                                        newTaskUnit.setType("0");//待签收
                                        newTaskUnit.setState("0");//待办
                                    }
                                    newTaskUnit.setTimeRound(maxCount + 1 + "");
                                    taskUnitService.saveUnit(newTaskUnit);
                                    //创建人员任务
                                    createTaskUser(taskUsers, newTaskUnit);

                                    //发送短信。查询当前单位的领导
                                    SysUserFilter userFilter = new SysUserFilter();
                                    userFilter.setRoleId("02");
                                    userFilter.setDeptId(taskUnit.getDeptId());
                                    List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                    for (SysLogin sysLogin : loginList) {
                                        SysUser sysUser = userService.getById(sysLogin.getSysId());
                                        String mobile = sysUser.getMobile();
                                        if (StringUtils.isNotBlank(mobile)) {
                                            smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                        }
                                    }

                                    taskMain.setTimeLimit(newTaskUnit.getTimeLimit());
                                }

                            }
                        } else if ("1".equals(timeLimit.getPeriod())) {
                            //如果是每周，判断是每周几。查询当前是周几
                            int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                            if (week < 0)
                                week = 7;
                            if (Integer.toString(week).equals(timeLimit.getWeek())) {
                                //如果当天是周期天。那就生成一个同样的单位任务
                                //查询当前第一轮 的子任务；
                                //下周的周一周日
                                Map<String, String> nextWeek = WeekDayUtil.getNextWeek(dateFormat.format(new Date()));
                                //下周周期天的日期
                                String[] dates = WeekDayUtil.getDates(nextWeek.get("beginDateNext"), nextWeek.get("endDateNext"), taskSub.getWeek());
                                for (TaskUnit taskUnit : taskUnits) {
                                    //查询当前单位任务进行了几轮
                                    int i = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());
                                    //取出上一轮督查任务
                                    List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), i);
                                    //上一轮督查任务的时限
                                    String limit = taskUnitList.get(0).getTimeLimit();
                                    //查询上一轮单位任务批转的用户
                                    String taskUnitId = taskUnitList.get(0).getSysId();
                                    List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);
                                    i++;
                                    //创建一个内容相同的单位任务
                                    TaskUnit newTaskUnit = new TaskUnit();
                                    BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                    newTaskUnit.setSysId(null);
                                    newTaskUnit.setOverdueDay(null);
                                    newTaskUnit.setTimeLimit(dates[0]);
                                    newTaskUnit.setScore(null);
                                    newTaskUnit.setStatus("2");
                                    if (ObjectUtils.isNotBlank(taskUsers)) {
                                        newTaskUnit.setType("1");//已批转
                                        newTaskUnit.setState("1");//已办
                                    } else {
                                        newTaskUnit.setType("0");//待签收
                                        newTaskUnit.setState("0");//待办
                                    }
                                    newTaskUnit.setTimeRound(i + 1 + "");
                                    taskUnitService.saveUnit(newTaskUnit);
                                    //发送短信。查询当前单位的领导
                                    SysUserFilter userFilter = new SysUserFilter();
                                    userFilter.setRoleId("02");
                                    userFilter.setDeptId(taskUnit.getDeptId());
                                    List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                    for (SysLogin sysLogin : loginList) {
                                        SysUser sysUser = userService.getById(sysLogin.getSysId());
                                        String mobile = sysUser.getMobile();
                                        if (StringUtils.isNotBlank(mobile)) {
                                            smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                        }
                                    }
                                    taskMain.setTimeLimit(newTaskUnit.getTimeLimit());
                                }
                            }
                        } else if ("2".equals(timeLimit.getPeriod())) {
                            //如果是月周期，判断当前天数。取出当前周期月的日
                            String month = timeLimit.getMonth();
                            int monthInt = Integer.parseInt(month);
                            //获取当前月的天数（本月最后一天日期）
                            int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                            //判断当周期是在本月是否有该日
                            if (monthInt > actualMaximum) {
                                //本月没有周期中的日期,如果当天是本月最后一天
                                if (day == actualMaximum) {
                                    //设置本次任务 的下月时限
                                    nowMonth = nowMonth + 1;
                                    if (nowMonth < 10) {
                                        monthStr = "0" + nowMonth;
                                    }
                                    String time = nowYear + "-" + monthStr + "-" + day;
                                    for (TaskUnit taskUnit : taskUnits) {
                                        //查询当前单位任务进行了几轮
                                        int i = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());
                                        //取出上一轮督查任务
                                        List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), i);
                                        //上一轮督查任务的时限
                                        String limit = taskUnitList.get(0).getTimeLimit();
                                        //查询上一轮单位任务批转的用户
                                        String taskUnitId = taskUnitList.get(0).getSysId();
                                        List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);
                                        //创建一个内容相同的单位任务
                                        TaskUnit newTaskUnit = new TaskUnit();
                                        BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                        newTaskUnit.setSysId(null);
                                        newTaskUnit.setOverdueDay(null);
                                        newTaskUnit.setTimeLimit(time);
                                        newTaskUnit.setScore(null);
                                        newTaskUnit.setStatus("2");
                                        if (ObjectUtils.isNotBlank(taskUsers)) {
                                            newTaskUnit.setType("1");//已批转
                                            newTaskUnit.setState("1");//已办
                                        } else {
                                            newTaskUnit.setType("0");//待签收
                                            newTaskUnit.setState("0");//待办
                                        }
                                        newTaskUnit.setTimeRound(i + 1 + "");
                                        taskUnitService.saveUnit(newTaskUnit);

                                        //发送短信。查询当前单位的领导
                                        SysUserFilter userFilter = new SysUserFilter();
                                        userFilter.setRoleId("02");
                                        userFilter.setDeptId(taskUnit.getDeptId());
                                        List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                        for (SysLogin sysLogin : loginList) {
                                            SysUser sysUser = userService.getById(sysLogin.getSysId());
                                            String mobile = sysUser.getMobile();
                                            if (StringUtils.isNotBlank(mobile)) {
                                                smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                            }
                                        }
                                        taskMain.setTimeLimit(newTaskUnit.getTimeLimit());
                                    }
                                }
                            } else {
                                //本月有这天,当今天是周期日的时候生成下月任务
                                if (day == monthInt) {
                                    String time = "";
                                    //如果是12月
                                    if (nowMonth == 12) {
                                        time = (nowYear + 1) + "-01-" + dayStr;
                                    } else if (nowMonth == 1 && monthInt >= 29) {
                                        //如果当前月是一月并且周期的日时间大于29，那么将下月的时限定为二月28
                                        time = nowYear + "-02-28";
                                    } else if (actualMaximum % 2 == 1 && monthInt > 30 && nowMonth != 1 && nowMonth != 12) {
                                        //如果本月是大月，并且周期的日为31号。下月的反馈时限定为30号
                                        time = nowYear + "-" + (nowMonth + 1) + "-30";
                                    } else {
                                        // .月时限
                                        time = nowYear + "-" + (nowMonth + 1) + "-" + dayStr;
                                    }
                                    //设置本次任务 的下
                                    for (TaskUnit taskUnit : taskUnits) {
                                        //查询当前单位任务进行了几轮
                                        int i = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());
                                        //取出上一轮督查任务
                                        List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), i);
                                        //上一轮督查任务的时限
                                        String limit = taskUnitList.get(0).getTimeLimit();
                                        //查询上一轮单位任务批转的用户
                                        String taskUnitId = taskUnitList.get(0).getSysId();
                                        List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);
                                        //创建一个内容相同的单位任务
                                        TaskUnit newTaskUnit = new TaskUnit();
                                        BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                        newTaskUnit.setSysId(null);
                                        newTaskUnit.setOverdueDay(null);
                                        newTaskUnit.setTimeLimit(time);
                                        newTaskUnit.setScore(null);
                                        newTaskUnit.setStatus("2");
                                        if (ObjectUtils.isNotBlank(taskUsers)) {
                                            newTaskUnit.setType("1");//已批转
                                            newTaskUnit.setState("1");//已办
                                        } else {
                                            newTaskUnit.setType("0");//待签收
                                            newTaskUnit.setState("0");//待办
                                        }
                                        newTaskUnit.setTimeRound(i + 1 + "");
                                        taskUnitService.saveUnit(newTaskUnit);
                                        //发送短信。查询当前单位的领导
                                        SysUserFilter userFilter = new SysUserFilter();
                                        userFilter.setRoleId("02");
                                        userFilter.setDeptId(taskUnit.getDeptId());
                                        List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                        for (SysLogin sysLogin : loginList) {
                                            SysUser sysUser = userService.getById(sysLogin.getSysId());
                                            String mobile = sysUser.getMobile();
                                            if (StringUtils.isNotBlank(mobile)) {
                                                smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                            }
                                        }
                                        taskMain.setTimeLimit(newTaskUnit.getTimeLimit());

                                    }
                                }
                            }
                        } else {
                            //如果是季度周期
                            for (TaskUnit taskUnit : taskUnits) {
                                //查询当前单位任务进行了几轮
                                int i = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());
                                //创建一个内容相同的单位任务
                                //取出上一轮督查任务
                                List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), i);
                                //上一轮督查任务的时限
                                String limit = taskUnitList.get(0).getTimeLimit();
                                //查询上一轮单位任务批转的用户
                                String taskUnitId = taskUnitList.get(0).getSysId();
                                List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);

                                if (timeNow.equals(limit)) {
                                    //如果当前日期是上个季度周期的日期相同 计算下个季度任务的 时限
                                  /*  long nowHm = dateFormat.parse(timeNow).getTime();//当前日期的毫秒值
                                    long result = nowHm + (24 * 60 * 60 * 1000 * 91); //一个季度后的毫秒值
                                    Date date = new Date(result);
                                    String format = dateFormat.format(date);*/
                                    String nextQuarterDay = DateUtil.nextQuarterDay(timeNow);
                                    //创建一个内容相同的单位任务
                                    TaskUnit newTaskUnit = new TaskUnit();
                                    BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                    newTaskUnit.setSysId(null);
                                    newTaskUnit.setOverdueDay(null);
                                    newTaskUnit.setTimeLimit(nextQuarterDay);
                                    newTaskUnit.setScore(null);
                                    newTaskUnit.setStatus("2");
                                    if (ObjectUtils.isNotBlank(taskUsers)) {
                                        newTaskUnit.setType("1");//已批转
                                        newTaskUnit.setState("1");//已办
                                    } else {
                                        newTaskUnit.setType("0");//待签收
                                        newTaskUnit.setState("0");//待办
                                    }
                                    newTaskUnit.setTimeRound(i + 1 + "");
                                    taskUnitService.saveUnit(newTaskUnit);
                                    //发送短信。查询当前单位的领导
                                    SysUserFilter userFilter = new SysUserFilter();
                                    userFilter.setRoleId("02");
                                    userFilter.setDeptId(taskUnit.getDeptId());
                                    List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                    for (SysLogin sysLogin : loginList) {
                                        SysUser sysUser = userService.getById(sysLogin.getSysId());
                                        String mobile = sysUser.getMobile();
                                        if (StringUtils.isNotBlank(mobile)) {
                                            smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                        }
                                    }
                                    taskMain.setTimeLimit(newTaskUnit.getTimeLimit());
                                }
                            }
                        }
                    } else if ("3".equals(timeLimit.getTimeType())) {
                        //判断是否有多时限
                        String limits = timeLimit.getTimeLimits();
                        if (limits.contains(",")) {
                            //如果是多时限，将时限转化为数组
                            String[] split = limits.split(",");
                            //循环数组
                            for (int i = 0; i < split.length; i++) {
                                //如果当前时间与时相同
                                if (split[i].equals(timeNow)) {
                                    //创建下个时限的任务
                                    if (i < split.length - 1) {
                                        //取当前时间的下一个时间作为新任务的时限
                                        String time = split[i + 1];
                                        for (TaskUnit taskUnit : taskUnits) {
                                            //查询当前单位任务进行了几轮
                                            int i1 = taskUnitService.getCountBySubDept(taskSub.getSysId(), taskUnit.getDeptId());

                                            //创建一个内容相同的单位任务
                                            //取出上一轮督查任务
                                            List<TaskUnit> taskUnitList = taskUnitService.getBySubDeptIdAndTimeRound(taskSub.getSysId(), taskUnit.getDeptId(), i);
                                            //上一轮督查任务的时限
                                            String limit = taskUnitList.get(0).getTimeLimit();
                                            //查询上一轮单位任务批转的用户
                                            String taskUnitId = taskUnitList.get(0).getSysId();
                                            List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnitId);
                                            //创建一个内容相同的单位任务
                                            TaskUnit newTaskUnit = new TaskUnit();
                                            BeanUtils.copyProperties(taskUnit, newTaskUnit);
                                            newTaskUnit.setSysId(null);
                                            newTaskUnit.setOverdueDay(null);
                                            newTaskUnit.setTimeLimit(time);
                                            newTaskUnit.setScore(null);
                                            newTaskUnit.setStatus("2");
                                            if (ObjectUtils.isNotBlank(taskUsers)) {
                                                newTaskUnit.setType("1");//已批转
                                                newTaskUnit.setState("1");//已办
                                            } else {
                                                newTaskUnit.setType("0");//待签收
                                                newTaskUnit.setState("0");//待办
                                            }
                                            newTaskUnit.setTimeRound(i1 + 1 + "");
                                            taskUnitService.saveUnit(newTaskUnit);
                                            //发送短信。查询当前单位的领导
                                            SysUserFilter userFilter = new SysUserFilter();
                                            userFilter.setRoleId("02");
                                            userFilter.setDeptId(taskUnit.getDeptId());
                                            List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                                            for (SysLogin sysLogin : loginList) {
                                                SysUser sysUser = userService.getById(sysLogin.getSysId());
                                                String mobile = sysUser.getMobile();
                                                if (StringUtils.isNotBlank(mobile)) {
                                                    smsUtilService.taskNewSmsService(taskUnit, sysUser);
                                                }
                                            }
                                            taskMain.setTimeLimit(newTaskUnit.getTimeLimit());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    taskMainService.update(taskMain);
                }
            }
        }
    }

    /**
     * 查询上一轮单位任务批转过没有
     *
     * @param taskUnitList
     * @return
     */
    public String createTaskUser(List<TaskUser> taskUnitList, TaskUnit taskUnit) {

        //查询上一轮单位任务批转的用户
        List<String> users = new ArrayList<>();
        String sysId = taskUnitList.get(0).getSysId();
        List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(sysId);
        if (ObjectUtils.isNotBlank(taskUsers)) {
            for (TaskUser taskUser : taskUsers) {
                String userId = taskUser.getUnitUserId();
                String mobile = userService.getById(userId).getMobile();
                TaskUser newTaskUser = new TaskUser();
                BeanUtils.copyProperties(taskUser, newTaskUser);
                taskUser.setTaskUnitId(taskUnit.getSysId());
                SysLogin login = sysLoginService.getById(userId);
                if ("03".equals(login.getRoleId())) {
                    //分管， 判断他是否批转过
                    List<TaskUser> taskUnit2 = taskUserService.getByCreateUserAndTaskUnitId(userId, taskUnitList.get(0).getSysId());
                    if (ObjectUtils.isNotBlank(taskUnit2)) {
                        //批转过，状态改为一批转
                        taskUser.setStatus("1");
                        taskUser.setState("1");//已办
                    } else {
                        taskUser.setStatus("0");//未处理
                        taskUser.setState("0");//待办
                    }
                } else {
                    //经办
                    taskUser.setStatus("0");//未处理
                    taskUser.setState("0");//待办
                }
                users.add(userId);
                taskUserService.saveTaskUser(taskUser);
                //发送批转短信
                smsUtilService.taskUserSMS(taskUnit, userService.getById(userId));
            }
            return "200";
        } else {
            return "500";
        }
    }
}
